/// 静态文件嵌入

use std::{borrow::{Borrow, Cow}, ops::Deref};

use rust_embed::RustEmbed;

/**
 * 会嵌入整个文件夹的所有文件
 * debug模式下会实时读取路径下的文件，release模式才会嵌入可执行文件
 */
#[derive(RustEmbed)]
#[folder = "static/"]
struct StaticFiles;

pub fn main() {
    // 读取嵌入的静态文件
    let content = StaticFiles::get("hello.html").unwrap().data;
 
    // 将静态文件内容转换为字符串
    let content_str = std::str::from_utf8(&content).unwrap();

    println!("{}", content_str);

    let content = StaticFiles::get("weigui.mp4").unwrap().data;

    let n = 10;
    static sn: i32 = 10;
    println!("n addr={:?}", &n as *const i32);
    println!("static n addr={:?}", &sn as *const i32);
    match content {
        Cow::Borrowed(cnt) => {
            println!("Borrowed content.0 addr={:?}", cnt as *const [u8]);
        },
        Cow::Owned(ref cnt) => {
            println!("Owned content.0 addr={:?}", cnt as *const Vec<u8>);
        }
    }
    
    let first_cnt: String = content[..10].iter().map(|b|char::from(*b)).collect();
    println!("mp4 content len:{} first 10 byte :{:?}", content.len(), first_cnt);

    //Cow练习，
    let arr = &[1,3,9,6,5];
    let mut a = Cow::Borrowed(arr);
    let own = a.to_mut();
    own[0] = 2;

    println!("arr={:?}",arr);
    println!("own={:?}", a.deref());

    //只嵌入单个文件
    let byt = include_str!("../../static/file.txt");
    println!("byt={} \nbyt end.", &byt[..100]);
    
}
